package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.ContactNote;
import entity.User;
import util.DBHelp;
import util.RowMapper;

public class ContactNoteDao {

	private DBHelp<ContactNote> db=new DBHelp<ContactNote>();
	
	public void save(ContactNote cn){
		String sql="insert into t_contactnote (content,createtime,user_id,contact_id) values (?,?,?,?)";
		db.executeSQL(sql, cn.getContent(),cn.getCreatetime(),cn.getUser_id(),cn.getContact_id());
	}
	public List<ContactNote> find(String id){
		String sql="SELECT content,createtime,user_id,contact_id,username FROM t_contactnote LEFT JOIN t_user ON t_contactnote.user_id=t_user.id where contact_id=?";
		return db.executeQueryToList(sql, new RowMapper<ContactNote>(){

			public ContactNote mapRow(ResultSet rs) throws SQLException {
				ContactNote cn=new ContactNote();
				cn.setContact_id(rs.getString("contact_id"));
				cn.setContent(rs.getString("content"));
				cn.setCreatetime(rs.getString("createtime"));
				cn.setUser_id(rs.getString("user_id"));
				
				User user=new User();
				user.setId(rs.getString("user_id"));
				user.setUsername(rs.getString("username"));
				
				cn.setUser(user);
				
				return cn;
			}
			
		},id);
	}
}
